package com.learn.practice;

import com.learn.practice.config.CacheConfiguration;
import io.lettuce.core.dynamic.annotation.Command;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.availability.AvailabilityChangeEvent;
import org.springframework.boot.availability.ReadinessState;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Component;

@EnableCaching
@SpringBootApplication
@Slf4j
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringPracticeApplication {

    public static void main(String[] args) {
        System.setProperty("LOG_PATH", "./logs");
        System.setProperty("LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE", "1KB");
        SpringApplication.run(SpringPracticeApplication.class, args);
    }

    @Component
    class SpringPracticeApplicationListener implements ApplicationListener<AvailabilityChangeEvent> {
        @Override
        public void onApplicationEvent(AvailabilityChangeEvent event) {
            System.out.println("监听到事件：" + event);
            if (ReadinessState.ACCEPTING_TRAFFIC == event.getState()) {
                System.out.println("应用启动完成，可以请求了……");
            }
        }
    }


    private static final Log logger1 = LogFactory
            .getLog(SpringPracticeApplication.class);

    private static final Logger logger2 = LoggerFactory
            .getLogger(SpringPracticeApplication.class);

    @Bean
    @Order(2)
    public CommandLineRunner commandLineRunner() {
        return (args) -> {
            logger1.error("commons logging error...");
            logger1.warn("commons logging warn");
            logger1.info("commons logging info...");
            logger1.debug("commons logging debug...");

            logger2.error("slf4j error...");
            logger2.warn("commons logging warn");
            logger2.info("slf4j info...");
            logger2.debug("slf4j debug...");
        };
    }

    @Bean
    public CommandLineRunner commandLineRunner2() {
        return (args) -> {
            AnnotationConfigApplicationContext context = new
                    AnnotationConfigApplicationContext(CacheConfiguration.class);
            for (String beanName : context.getBeanDefinitionNames()) {
                log.info(beanName + "->" + context.getBean(beanName));
            }
        };
    }
}
